package cn.qiuyu.gift.controller;

import cn.qiuyu.gift.dto.PhoneCodeDto;
import cn.qiuyu.gift.dto.UserRegisterDto;
import cn.qiuyu.gift.service.IUserService;
import cn.qiuyu.gift.domain.User;
import cn.qiuyu.gift.query.UserQuery;
import cn.qiuyu.gift.util.AjaxResult;
import cn.qiuyu.gift.util.PageList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.plugins.Page;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public IUserService userService;


    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){

        if( user.getId()!=null)
            userService.updateById(user);
        else
            userService.insert(user);
        return AjaxResult.success();

    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
            userService.deleteById(id);
        return AjaxResult.success();

    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {

        User user = userService.selectById(id);
        return AjaxResult.success(user);

    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        List< User> list = userService.selectList(null);
        return AjaxResult.success(list);

    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody UserQuery query)
    {
        Page<User> page = new Page<User>(query.getPage(),query.getRows());
        page = userService.selectPage(page);
        PageList pageList = new PageList<User>(page.getTotal(),page.getRecords());
        return AjaxResult.success(pageList);
    }

    @GetMapping("/imgcode/{uuid}")
    public AjaxResult imgcode(@PathVariable("uuid") String uuid){
        System.out.println("56465");
        String ea = userService.createimgcode(uuid);
        return AjaxResult.success(ea);
    }

    @PostMapping("/phonecode")
    public AjaxResult phonecode(@RequestBody PhoneCodeDto phoneCode){
        userService.phonecode(phoneCode);
        System.out.println(phoneCode);
        return AjaxResult.success();

    }


    @PutMapping("/userregister")
    public AjaxResult userregister(@RequestBody UserRegisterDto user){

        System.out.println(user);
        userService.userregister(user);
        return AjaxResult.success();
    }



}
